<?php
class Product_model extends CI_Model{
	
	function __construct() {
        parent::__construct();
    }
    
    public function get_all_product(){
   	 	$sql = "SELECT * FROM product";
    	$query = $this->db->query($sql);
    	if ($query->num_rows > 0){
    		return $query->result_array();
    	}else{
    		return FALSE;
    	}
    }
    
    public function get_product($id){
    	$sql = "SELECT * FROM product WHERE product_id = {$id}";
    	$query = $this->db->query($sql);
    	if ($query->num_rows > 0){
    		return $query->result_array();
    	}else{
    		return FALSE;
    	}
    }
    
    public function get_product_details($id){
    	$sql = "SELECT * FROM product, seller WHERE product.seller_id = seller.seller_id AND product_id = {$id}";
    	$query = $this->db->query($sql);
    	if ($query->num_rows > 0){
    		return $query->result_array();
    	}else{
    		return FALSE;
    	}
    }
    
    public function get_product_by_category($category_id, $offset = 0){
    	$category_id = intval($category_id);
    	$offset = intval($offset);
    	$limit = 4;
    	$sql = "SELECT * FROM product, seller WHERE product.seller_id = seller.seller_id AND 
    			category_id = {$category_id} LIMIT {$offset}, {$limit}";
    	$query = $this->db->query($sql);
    	if ($query->num_rows > 0){
    		return $query->result_array();
    	}else{
    		return FALSE;
    	}
    }
    
    public function search_product($key, $offset = 0, $limit = 0){
    	$key = mysql_real_escape_string($key);
    	$offset = intval($offset);
    	$limit = intval($limit);
    	if ($limit == 0){
    		$sql = "SELECT * FROM product, seller WHERE product.seller_id = seller.seller_id AND 
    			(product_name LIKE '%{$key}%' OR product_feature LIKE '%{$key}%')";
    	}else{
    		$sql = "SELECT * FROM product, seller WHERE product.seller_id = seller.seller_id AND 
    			(product_name LIKE '%{$key}%' OR product_feature LIKE '%{$key}%') LIMIT {$offset}, {$limit}";
    	}    	
    	$query = $this->db->query($sql);
    	if ($query->num_rows > 0){
    		return $query->result_array();
    	}else{
    		return FALSE;
    	}
    }
}

/* End of file product_model.php */
/* Location: ./application/models/product_model.php */